package userwashlogic

import (
	"context"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/userwash"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/service"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserWashBetLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserWashBetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserWashBetLogic {
	return &UserWashBetLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

var TypeNameToCategoryCode = map[string][]string{
	"cp_xima": {"Lottery"},
	"ty_xima": {"Sport"},
	"qp_xima": {"Chess"},
	"zr_xima": {"Video"},
	"dz_xima": {"Slot", "Fish", "Flash"},
}

// rpc userWash 用户洗码
func (l *UserWashBetLogic) UserWashBet(in *rpc.UserWashBetRequest) (*rpc.UserWashBetResponse, error) {
	resp := &rpc.UserWashBetResponse{}

	var err error
	resp.BetValue, err = userwash.NewUserWashBetModel(l.ctx, l.svcCtx.Db).GetBetValue(in.UserId, in.TypeName)
	if err != nil {
		return nil, err
	}
	resp.TodayWash, err = userwash.NewUserWashModel(l.ctx, l.svcCtx.Db).GetTodayWash(in.UserId, in.TypeName)
	if err != nil {
		return nil, err
	}
	resp.TotalWash, err = userwash.NewUserWashModel(l.ctx, l.svcCtx.Db).GetTotalWash(in.UserId, in.TypeName)
	if err != nil {
		return nil, err
	}

	resp.Status = service.NewUserWashService(l.ctx, l.svcCtx).IsOpenWash()
	return resp, nil
}

var CategoryCodeToTypeName = map[string]string{
	"Lottery": "cp_xima",
	"Sport":   "ty_xima",
	"Chess":   "qp_xima",
	"Video":   "zr_xima",
	"Slot":    "dz_xima",
	"Fish":    "dz_xima",
	"Flash":   "dz_xima",
}

// AssetsConsumeBet 消费用户 bet topic
func (l *UserWashBetLogic) AssetsConsumeBet(eventType string, amount float64, userId int64, categoryCode string) error {
	if eventType == "" || userId == 0 || amount == 0 || categoryCode == "" {
		return nil
	}
	l.Logger.Info("AssetsConsumeBet,info", eventType, amount, userId, categoryCode)

	typeName, ok := CategoryCodeToTypeName[categoryCode]
	if !ok {
		l.Logger.Info("CategoryCodeToTypeName,typeName not found")
		return nil
	}

	switch eventType {
	case "bet":
		return userwash.NewUserWashBetModel(l.ctx, l.svcCtx.Db).BetIncrease(amount, userId, categoryCode, typeName)
	case "bet_cancel":
		return userwash.NewUserWashBetModel(l.ctx, l.svcCtx.Db).BetDecrease(amount, userId, categoryCode, typeName)
	}
	return nil
}
